package com.lzs.baseruku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.baseruku.service.WarehouseService;
import com.lzs.entity.Warehouse;
import com.lzs.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 仓库管理 前端控制器
 * </p>
 *
 * @author ymx
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/baseruku/warehouse")
@Api(value = "仓库管理文档")
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;



    @ApiOperation(value = "查询不等于某个id的所有的仓库")
    @PostMapping("/selectAllWarehouse/{warehouseid}")
    public List<Warehouse> selectAllWarehouse(@PathVariable("warehouseid") Long warehouseid){
        QueryWrapper<Warehouse> queryWrapper=new QueryWrapper<>();
        queryWrapper.notIn("id",warehouseid);
        return warehouseService.list(queryWrapper);
    }

    @ApiOperation(value = "查询所有的仓库")
    @PostMapping("/selectWarehouse/{page}/{limit}")
    public R selectWarehouse(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Warehouse warehouse){
        Page<Warehouse> warehousePage = warehouseService.selectWarehouse(page, limit, warehouse);
        return R.ok().data("warehousePage",warehousePage);
    }

    @ApiOperation(value = "根据ID查询仓库")
    @GetMapping("/selectWarehouseById/{id}")
    public R selectWarehouseById(@PathVariable Long id){
        Warehouse warehouse = this.warehouseService.getById(id);
        return R.ok().data("warehouse",warehouse);
    }

    @ApiOperation(value = "新增或修改仓库")
    @PostMapping("/insertOrUpdateWarehouse")
    public R insertOrUpdateWarehouse(@RequestBody Warehouse warehouse){
            boolean b = this.warehouseService.saveOrUpdate(warehouse);
            if (b){
                return R.ok().message("操作成功");
            }else {
                return R.error().message("操作失败");
            }
    }

    @ApiOperation(value = "根据ID删除仓库")
    @DeleteMapping("/deleteWarehouse/{id}")
    public R deleteWarehouse(@PathVariable Long id){
        boolean b = this.warehouseService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

}

